package com.aizhixin.lab.project.course.service;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.course.domain.DeleteDccumentIdsDomain;
import com.aizhixin.lab.project.course.domain.ProjectDocumentCheckDomain;
import com.aizhixin.lab.project.course.domain.ProjectDocumentDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectDocumentDomain;
import com.aizhixin.lab.project.course.entity.ProjectDocument;
import com.aizhixin.lab.project.course.repository.ProjectDocumentRepository;
import com.aizhixin.lab.project.course.vo.ProjectDocumentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectDocumentService {
    @Autowired
    private ProjectDocumentRepository projectDocumentRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    RowMapper<ProjectDocumentVo> documentVoRowMapper = new RowMapper<ProjectDocumentVo>() {

        @Override
        public ProjectDocumentVo mapRow(ResultSet rs, int rowNum) throws SQLException {

            ProjectDocumentVo domain = new ProjectDocumentVo();
            domain.setId(rs.getString("id"));
            domain.setCheckStatus(rs.getInt("check_status"));
            domain.setDocumentName(rs.getString("document_name"));
            domain.setDocumentPath(rs.getString("document_path"));
            domain.setCreatedBy(rs.getString("created_by"));
            domain.setCreatedDate(rs.getString("created_date"));
            domain.setCheckSuggest(rs.getString("check_suggest"));
            domain.setFileKey(rs.getString("file_key"));
            domain.setFileName(rs.getString("file_name"));
            return domain;
        }
    };

    public ProjectDocument save(ProjectDocumentDomain domain) {
        ProjectDocument projectDocument = new ProjectDocument();
        BeanUtils.copyProperties(domain, projectDocument);
        projectDocument.setId(UUID.randomUUID().toString());
        projectDocument.setCreatedDate(new Date());
        projectDocument.setDeleteFlag(DataValidity.VALID.getState());
        projectDocument.setCreatedBy(domain.getCreatedBy());
        projectDocument.setCheckStatus(DataValidity.VALID.getState());
        return projectDocumentRepository.save(projectDocument);
    }


    public ProjectDocument check(ProjectDocumentCheckDomain domain) {
        ProjectDocument projectDocument = projectDocumentRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain, projectDocument);
        projectDocument.setCheckTime(new Date());
        return projectDocumentRepository.save(projectDocument);
    }

    public void delete(DeleteDccumentIdsDomain ids) {
        for (String id:ids.getList()) {
            ProjectDocument projectDocument = projectDocumentRepository.findOne(id);
            ProjectDocument document = new ProjectDocument();
            BeanUtils.copyProperties(projectDocument, document);
            document.setDeleteFlag(DataValidity.INVALID.getState());
            projectDocumentRepository.save(document);
        }
    }

    public <T> Map<String, Object> findProjectDocument(QueryProjectDocumentDomain domain) {
        String sql = "SELECT a.id,a.document_name,a.document_path,a.check_status,a.file_name,a.file_key,a.check_suggest,a.created_date,b.NAME as created_by FROM t_project_document a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '" + domain.getProjectId() + "'and a.delete_flag = 0";
        String count = "SELECT count(*) FROM t_project_document a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '" + domain.getProjectId() + "'and a.delete_flag = 0 order by a.check_time desc limit 0,10";

        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto = new SortDTO();
        dto.setKey("a.check_time");
        dto.setAsc(false);
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(),domain.getPageNumber(),documentVoRowMapper,sort,sql,count);
    }


}
